package penguin.masking.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class MetaHandlerFactory {

    MysqlMetaHandler mysqlMetaHandler = new MysqlMetaHandler();
    AbstractDBMetaHandler abstractDBMetaHandler = new AbstractDBMetaHandler();
    OracleMetaHandler oracleMetaHandler = new OracleMetaHandler();

    private static final MetaHandlerFactory factory = new MetaHandlerFactory();

    MetaHandlerFactory() {

    }

    public static MetaHandlerFactory getInstance() {
        return factory;
    }

    public DBMetaHandler handler(String url) {
        String _url = url.toLowerCase();
        if (_url.startsWith("jdbc:mysql:")) {
            return mysqlMetaHandler;
        } else if (_url.startsWith("jdbc:oracle:")) {
            return oracleMetaHandler;
        } else if (_url.startsWith("jdbc:postgresql:")) {

        }
        return abstractDBMetaHandler;
    }

    public DBMetaHandler handler(ResultSet set) {
        try {
            String url = set.getStatement().getConnection().getMetaData().getURL();
            return handler(url);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
